package com.sdau.zhang.controller;

import com.sdau.zhang.dao.LuntanDao;
import com.sdau.zhang.service.impl.LuntanServiceImpl;
import com.sdau.zhang.vo.Luntan;
import com.sdau.zhang.vo.Response;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/luntan")
@Controller
public class LuntanController {

    @Autowired
    private LuntanServiceImpl luntanService;

    @Autowired
    private LuntanDao luntanDao;

    @RequestMapping("/enter")
    public String luntan(Model model, int tid)
    {
        List<Luntan> ll = luntanService.getByTid(tid,0,10);
        model.addAttribute("luntanList",ll);
        Map<Integer,List<Response>> ml = luntanService.getResponse(1,1);
        model.addAttribute("ml",ml);
        model.addAttribute("curr",1);
        return "Mr_z/luntan.html";
    }

    @RequestMapping(value="shift",method = RequestMethod.POST)
    public @ResponseBody String shift(@RequestBody String data,Model model)
    {
        JSONObject jsonObject = JSONObject.fromObject(data);
        int page = (int)jsonObject.get("curr");
        int limitCount = (int)jsonObject.get("limitCount");
        int tid =(int)jsonObject.get("tid");
        //写入所需数据
        return "http://localhost:8000/luntan/other?tid="+tid+"&page="+page;
    }
    @RequestMapping(value="other",method = RequestMethod.GET)
    public String shift(int tid,int page,Model model){

        int begin = (page-1)*10;
        List<Luntan> ll = luntanService.getByTid(tid,begin,10);
        model.addAttribute("luntanList",ll);
        Map<Integer,List<Response>> ml = luntanService.getResponse(tid,page);
        model.addAttribute("ml",ml);
        model.addAttribute("curr",page);
        System.out.println(page);
        return "Mr_z/luntan.html";
    }


}
